/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elektra;

import com.mongodb.Mongo;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;


/**
 *
 * @author adee
 */
@Deprecated
public class Elektra_NOTUSED {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws UnknownHostException, XMPPException, InterruptedException, IOException {
/*
       Mongo m = new Mongo();
       DB db = m.getDB( "mydb" );
       DBCollection coll = db.getCollection("testCollection");

        BasicDBObject doc = new BasicDBObject();

        doc.put("name", "ListTest");
        doc.put("type", "database");
        doc.put("count", 1);

        BasicDBObject info = new BasicDBObject();

        info.put("x", 203);
        info.put("y", 102);
        
        ArrayList list = new ArrayList();
        list.add("Kutya");
        list.add("Cica");
        list.add("alma");
        info.put("List", list);

        doc.put("info", info);
        coll.insert(doc);
        DBObject myDoc = coll.findOne(new BasicDBObject("name", "ListTest"));
        System.out.println(myDoc);
        System.out.println(coll.count());
        Set<String> colls = db.getCollectionNames();

        for (String s : colls) {
           System.out.println(s);
        }
       
       
  */  
        
    String apiKey="353574954653814";
    String accessToken = "";
    ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222);
    config.setSASLAuthenticationEnabled(true);
    XMPPConnection  mFbConnection = new XMPPConnection(config);
    final Process process = Runtime.getRuntime ().exec ("/bin/sh");

    
    

try {
    SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM", SASLXFacebookPlatformMechanism.class);
    SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0);
    mFbConnection.connect();
    System.out.println(mFbConnection.isConnected());
    mFbConnection.login(apiKey, accessToken, "Application");
// Assume we've created a Connection name "connection".
        System.out.println(mFbConnection.isAuthenticated());

ChatManager chatmanager = mFbConnection.getChatManager();
Chat chat;
chatmanager.addChatListener(
    new ChatManagerListener() {
        @Override
        public void chatCreated(Chat chat, boolean createdLocally)
        {
            
            chat.addMessageListener(new MessageListener() {

                        @Override
                        public void processMessage(Chat chat, Message msg) {
                            
                            System.out.println(msg.getBody());
                            try {
                                             InputStream stdout = process.getInputStream();
                                             OutputStream stdin= process.getOutputStream();
                                             
                                             BufferedWriter writer= new BufferedWriter(new OutputStreamWriter(stdin));
                                try {
                                    writer.append(msg.getBody().trim());
                                    writer.newLine();
                                    writer.flush();
                                try {
                                    Thread.sleep(2000);
                                } catch (InterruptedException ex) {
                                    Logger.getLogger(Elektra_NOTUSED.class.getName()).log(Level.SEVERE, null, ex);
                                }
                                
                                
                                BufferedReader reader = new  BufferedReader(new InputStreamReader(stdout));
                                
                                String line=null;
                                String mess = "";
                                while ((line=reader.readLine())!=null){
                                              System.out.println("Line:"+line);
                                              chat.sendMessage(line);

                                }
                                
                                
                                
                                } catch (IOException ex){
                                    
                                    System.out.println(ex);
                   
                                }
                                System.out.println(msg.getBody());
                            } catch (XMPPException ex) {
                                Logger.getLogger(Elektra_NOTUSED.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    });
                    
                    
            
            
          System.out.println(chat);
                    try {
                        chat.sendMessage("Üdv, már is csinálom :D .");
                          // if (!createdLocally)
                          //    chat.addMessageListener(new MyNewMessageListener());;
                    } catch (XMPPException ex) {
                        Logger.getLogger(Elektra_NOTUSED.class.getName()).log(Level.SEVERE, null, ex);
                    }
        }
    });

process.waitFor();

} catch (XMPPException e) {
    mFbConnection.disconnect();
    e.printStackTrace();
}
        
        
        
        
    }
}
